<?php
	class AdminController extends AppController {
		
		var $name = 'Admin';
		var $layout = "principal";
		var $components = array('UserSession');
		var $uses = Array('Usuario');

		function index(){
			$user = $this->UserSession->get();
			$this->layout = 'admin';

			//busco los posts del usuario
// 			$posts = $this->Post->find('all',array('conditions'=>array('Post.usuario_id'=>$user['Usuario']['id']),'order'=>'Post.created DESC'));

			$this->set('user',$user);
// 			$this->set('posts',$posts);
		}

		function login(){

		}

		function logout(){
			$this->UserSession->stop();
// 			$this->redirect(array('controller' => 'posts', 'action'=> 'index'));exit;
		}

		function loguear(){

				if($this->data != null){
// 					$this->Usuario->unbindModel(array('hasMany'=>array('Post','Comentario')));
					$user = $this->Usuario->find('first',array('conditions' => array('Usuario.username'=>$this->data['username'],'Usuario.pass'=>md5($this->data['pass']))));

					if($user){
						$this->UserSession->stop();
						$this->UserSession->start($user);

						$this->Session->setFlash("Bienvenido ".$user['Usuario']['username'], 'default', array('class' => 'general_msg'), 'general_msg');

						$this->redirect(array('controller'=>'administrar','action'=>'index'));exit;
					}else{
						$this->Session->setFlash('Nombre de usuario o contraseña incorrecto', 'default', array('class' => 'general_error'), 'general_error');
						$this->redirect(array('controller'=>'administrar','action'=>'login'));exit;
					}
				}else{
					$this->Session->setFlash('Error inseperado', 'default', array('class' => 'general_error'), 'general_error');
					$this->redirect(array('controller'=>'posts','action'=>'index'));exit;
				}
				exit;
		}

	}
?>